package com.king01.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.king01.dao.InventoryDao;
import com.king01.entity.Inventory;
import com.king01.entity.InventoryGoods;
import com.king01.entity.Staff;
import com.king01.service.InventoryService;
import com.king01.vo.GoodsVo;
import com.king01.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author：张梓康
 * @date：Created in 2023/10/11 9:46
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    private HttpSession session;


    @Override
    public Result insert(List<Inventory> inventoryList) {
        Staff staff = (Staff) session.getAttribute("staff");
        Inventory inventory = new Inventory();
        inventory.setInventime(new Date());
        inventory.setBranchid(staff.getBranchid());
        inventory.setInvenstaffid(staff.getStaffid());
        inventory.setState(1);
        inventoryDao.addInventory(inventory);
        for (Inventory inventory1 : inventoryList) {
            inventory1.setInvenid(inventory.getInvenid());
            inventoryDao.addInventory_Goods(inventory1);
        }
        return new Result(200, "添加盘点成功", null);
    }

    @Override
    public Result findAll(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Inventory> list = inventoryDao.selectAll();
        PageInfo<Inventory> pageInfo = new PageInfo<>(list);
        return new Result(200, "获取所有盘点信息成功", pageInfo);
    }

    @Override
    public Result selectAll(Integer currentPage, Integer pageSize, Inventory inventory) {
        PageHelper.startPage(currentPage,pageSize);
        Integer branchid = inventory.getBranchid();
        List<Inventory> list = inventoryDao.findAll(branchid);
        PageInfo<Inventory> pageInfo = new PageInfo<>(list);
        return new Result(200,"模糊查询成功",pageInfo);
    }

    @Override
    public Result findAllBySt(Integer currentPage, Integer pageSize,String[] date,Integer branchid) {
        PageHelper.startPage(currentPage,pageSize);
        String startTime = null;
        String endTime = null;
        if (date.length>0){
            startTime = date[0];
            endTime = date[1];
        }
        List<Inventory> list = inventoryDao.selectAllBySt(startTime,endTime,branchid);
        PageInfo<Inventory> pageInfo = new PageInfo<>(list);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result passInventory(Inventory inventory) {
        Date date = new Date();
        inventory.setExamtime(date);
        Staff staff = (Staff) session.getAttribute("staff");
        inventory.setExamstaffid(staff.getStaffid());
        Integer integer = inventoryDao.passInventory(inventory);
        Integer invenid = inventory.getInvenid();
        List<InventoryGoods> countsAndGoodsId = inventoryDao.findCountsAndGoodsId(invenid);
        for (InventoryGoods inventoryGoods:countsAndGoodsId){
            Integer goodsid = inventoryGoods.getGoodsid();
            Integer counts = inventoryGoods.getInvennum();
            Integer branchid = inventoryGoods.getBranchid();
            inventoryDao.updateGoodscounts(counts,goodsid,branchid);
        }
        return new Result(200, "审核盘点通过成功", integer);
    }

    @Override
    public Result rejectInventory(Inventory inventory) {
        Date date = new Date();
        inventory.setExamtime(date);
        Staff staff = (Staff) session.getAttribute("staff");
        inventory.setExamstaffid(staff.getStaffid());
        Integer integer = inventoryDao.rejectInventory(inventory);
        return new Result(200, "审核盘点驳回成功", integer);
    }

    @Override
    public Result findAllXQ(Integer currentPage, Integer pageSize,Integer invenid) {
        PageHelper.startPage(currentPage,pageSize);
        List<InventoryGoods> goods = inventoryDao.findAllInventGoodsById(invenid);
        PageInfo<InventoryGoods> pageInfo = new PageInfo<>(goods);
        return new Result(200,"获取所有详情信息成功",pageInfo);
    }
}
    